package com.example.lab1.api.controller.v1;

import lombok.Data;
import lombok.ToString;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Holinc
 * @date 2023/3/23 19:53
 */
@RestController
@RequestMapping("/v1/lab1")
public class Lab1Controller {
    /**
     * ● GET：接口传入id参数，后端返回id*100后的结果
     * ● POST：接口传入{"name":"张三","sex":"男","age":18}，后端返回“张三，男性，18周岁”
     * ● PUT：接口传入{"name":"张三","sex":"男","age":18}，后端返回{"name":"张三","sex":"男","age":18,"mark":"已成年"}
     * ● DELETE： 接口传入{"name":"张三","sex":"男","age":18}，后端返回，“张三信息已删除”
     */


//    /v1/lab1?id=1&name=John
    @GetMapping("/id")
//    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public ResponseEntity<?> get(@RequestParam("id") int id) {
        System.out.println(id);
        int result = id * 10;
        return ResponseEntity.ok(result);
    }

    // 错误示例：
    // 正确需要返回ResponseEntity类型
//    @GetMapping("/idd")
//    public int getInt(@RequestParam("id") int id) {
//        System.out.println(id);
//        int result = id * 10;
//        return result;
//    }

    @PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> post(@RequestBody User user) {
        System.out.println(user);
        StringBuilder result = new StringBuilder();
        result.append(user.getName()).append("，").append(user.getSex()).append("，").append(user.getAge());
        return ResponseEntity.ok(result);
    }

    @PutMapping("/put")
    public ResponseEntity<?> put(@RequestBody User user) {
        System.out.println(user);
        user.setMark("已成年");
        return ResponseEntity.ok(user);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody User user, @RequestParam("id") int id) {
        System.out.println(id);
        System.out.println("==============");
        System.out.println(user);
        String result = "张三信息已删除";
        return ResponseEntity.ok(result);
    }

    @Data
    @ToString
    static class User {
        private String name;
        private String sex;
        private int age;
        private String mark;
    }

}
